package servlet;

import hbase.HbaseLink;
import hbase.HbaseUtil;
import hbase.TheMainTable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(value = "/AddStudent")
public class AddStudent extends HttpServlet {

    static Configuration conf;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



    String id = req.getParameter("booksId");
    String  name =req.getParameter("name");
    String  phone =req.getParameter("phone");
    String date = req.getParameter("date");
    String ses = req.getParameter("ses");
    String status =req.getParameter("bookStatus");

//        List<String> obj_value = (new UserLogFromHBase()).getUserLogList("Student", new String[]{"StuInfo","Grades",});

    ArrayList<TheMainTable> studentArrayList = new ArrayList<>();
    TheMainTable student = new TheMainTable(id ,name,date,phone,ses,status);

    studentArrayList.add(student);


    HbaseUtil hbaseAddUtil = new HbaseUtil();
    String obj_value = "";
        try {

        obj_value = hbaseAddUtil.insertinfo("niit01","2181","student",studentArrayList);
    }catch (Exception e){
        e.printStackTrace();
    }

        System.out.println("===="+obj_value);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
    //
//    public AddStudent() {
//        HbaseLink hbaselink = new HbaseLink();
//        Configuration conf = HbaseLink.connect();
//        this.conf = conf;
//    }

//    public static int add(TheMainTable studentTable) throws IOException {
//        HTable hTable = new HTable(conf, "student");
//        int flag = 0;
//        Scan scan= new Scan();
//        scan.addFamily(Bytes.toBytes("info"));
//        scan.addFamily(Bytes.toBytes("status"));
//        ResultScanner rs = hTable.getScanner(scan);
////        Connection.getTable("")
//        for (Result r : rs)
//        {
//            if (Bytes.toString(r.getRow()).equals(studentTable.getstudentid()))
//            {
//                flag = 1;
//                break;
//            }
//
//        }
//
//        if (flag == 0) {
//            Put put = new Put(Bytes.toBytes(studentTable.getstudentid()));
//            put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(studentTable.getname()));
//            put.add(Bytes.toBytes("info"), Bytes.toBytes("date"), Bytes.toBytes(studentTable.getdate()));
//            put.add(Bytes.toBytes("info"), Bytes.toBytes("phonenumber"), Bytes.toBytes(studentTable.getphonenumber()));
//            put.add(Bytes.toBytes("status"), Bytes.toBytes("semster"), Bytes.toBytes(studentTable.getsemster()));
//            put.add(Bytes.toBytes("status"), Bytes.toBytes("status"), Bytes.toBytes(studentTable.getsemster()));
//            hTable.put(put);
//        }
//        hTable.close();
//        return flag;
//
//    }


}
